<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
class PaisesController extends AppController{
public $components =array('Paginator','RequestHandler','DataTable');
public $uses=array('Paise',
        'User');
public $layout='sib';

function index (){
    $paises= $this->Paise->find ('all', array('recursive'=>-1));
    $this->set(compact('paises'));
    
}

function insertar(){
    /* if (!empty($this->request->data)) {
            //debug($this->request->data);            die;
            $this->Paise->create();          
            if ($this->Paise->save($this->request->data)) {
                $this->Session->setFlash('El pais ha sido guardado',mensajeBueno);
                $this->redirect(array('action' => 'index'), null, true);
            } else {
                $this->Session->setFlash('No se pudo guardar',mensajeError);
            }
        }*/
    
    if ($this->request->is('post')) {
        //debug($this->request->data); die;
         $valida = $this->validar('Paise');
            //debug($valida); die;
            if(empty($valida))
            {
                $this->Paise->create();
                if ($this->Paise->save($this->request->data['Paise'])) {
                    $this->Session->setFlash('El Pais fue registrado con exito con exito','msgbueno');
                    $this->redirect(array('controller'=>'Paises','action' => 'index'));
                } else {
                    $this->Session->setFlash('No se pudo registrar el Pais','msgerror');
                    $this->redirect(array('controller'=>'Paises','action' => 'index'));
                }
            }
            else{
                $this->Session->setFlash($valida,'msgerror');
                $this->redirect(array('controller'=>'Paises','action' => 'index'));
            }
        }
}

function editar($id = null) {

        $this->Paise->id = $id;
        if (!$this->Paise->exists()) {
            throw new NotFoundException(__('Invalido'));
        }
        if ($this->request->is('post')) {
            //debug($this->request->data);exit;
            
            if ($this->Paise->save($this->request->data)) {
                $this->Session->setFlash('El pais se edito correctamente','msgbueno');
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash('Error. Intentelo de nuevo','msgerror');
            }
        }else{
            $this->request->data = $this->Paise->read(null, $id);
        }
    }

 function eliminar($id) {
        if ($this->request->data('post')) {
            throw new MethodNotAllowedException();
        }
        if ($this->Paise->delete($id)) {
            $this->Session->setFlash('El Pais' . $id . 'Ha sido eliminado','msgbueno');
            $this->redirect(array('action' => 'index'));
        }
    }
}
?>
